﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace HttpDownloader
{
    public partial class AddURL : Form
    {
        public string strFilePath;
        public string strUrl;
        private bool isDirectLink;
        private bool isMediafire;
        private bool isMegaupload;
        private bool isPassFirstTime;
        private Thread m_Thread;

        public AddURL()
        {
            InitializeComponent();
            strFilePath = "";
            strUrl = "";
            isDirectLink = false;
            isMediafire = false;
            isPassFirstTime = false;
            m_pTxtURL.Text = Clipboard.GetText();
        }

        private void m_pBtnBrowse_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|Mp3 Files|*.Mp3|Wma Files|*.wma|Pdf Files|*.pdf|Winzar Files|*.rar|All Files|*.*";
            saveFileDialog1.Title = "Save to file";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                m_pTxtSaveAs.Text = saveFileDialog1.FileName;                
                strFilePath = saveFileDialog1.FileName;
            }
        }

        private void m_pTxtURL_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void m_pBtnDownload_Click(object sender, EventArgs e)
        {
            if (m_pTxtURL.Text.Equals(""))
            {
                MessageBox.Show("Hay nhap link de download");
            } 
            else
            {
                beginDownload();
            }
            
        }

        private void beginDownload()
        {
            isMediafire = false;
            isMegaupload = false;
            isDirectLink = false;

            if (m_pTxtURL.Text.Contains("mediafire.com") && !m_pTxtURL.Text.Contains("download"))  //kiem tra neu la link mediafire thi parse
            {
                isMediafire = true;
            }
            else if (m_pTxtURL.Text.Contains("megaupload.com") && !m_pTxtURL.Text.Contains("download"))
            {
                isMegaupload = true;
            }
            //xu ly mediafire, megaupload
            if ((isMegaupload || isMediafire) && !isDirectLink)
            {
                try
                {
                    m_fakeWebBrowser.ScriptErrorsSuppressed = true;
                    m_fakeWebBrowser.Navigate(m_pTxtURL.Text);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error");
                }

                MessageBox.Show("Please wait... If you want to abort, you can click button Cancel");
                if (isMegaupload)
                {
                    MessageBox.Show("Please wait 45s");
                }
            }
            else
            {
                finishGetLink();
            }

        }

        private void finishGetLink()
        {
            strFilePath = m_pTxtSaveAs.Text;
            strUrl = m_pTxtURL.Text;
            this.Close();
        }

        private void m_pBtnCancel_Click(object sender, EventArgs e)
        {
            strFilePath = "";
            strUrl = "";
            this.Close();
        }

        private void m_pTxtURL_Leave(object sender, EventArgs e)
        {
                 
        }

        private void m_fakeWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (isMediafire)
            {
                String newValue = m_pTxtPasswordUnlock.Text;

                HtmlElement txtPass = m_fakeWebBrowser.Document.GetElementById("downloadp");

                HtmlElement frmPass = m_fakeWebBrowser.Document.GetElementById("form_password");

                if (frmPass.Id == "form_password" && !isPassFirstTime)
                {

                    txtPass.SetAttribute("value", newValue);

                    frmPass.InvokeMember("submit");

                    frmPass.SetAttribute("status", "disable");

                    isPassFirstTime = true;

                }

                foreach (HtmlElement he in m_fakeWebBrowser.Document.Links)
                {
                    if (he.Parent.Style == "DISPLAY: block")
                    {

                        string ht = he.OuterHtml;

                        string link1 = (ht.Remove(0, ht.IndexOf("http")));

                        string link = link1.Remove(link1.IndexOf("\">"));

                        m_pTxtURL.Text = link;
                        isDirectLink = true;

                        finishGetLink();
                    }
                }
            } //ket thuc duyet mediafire
            
            else if (isMegaupload)
            {
                Thread.Sleep(45000);
                foreach (HtmlElement he in m_fakeWebBrowser.Document.Links)
                {
                    string val = he.GetAttribute("ClassName");  
                    if (he.Parent.Id == "downloadlink")
                    {
                        string ht = he.OuterHtml;
                        string link1 = (ht.Remove(0, ht.IndexOf("http")));
                        string link = link1.Remove(link1.IndexOf("\">"));

                        m_pTxtURL.Text = link;
                        isDirectLink = true;
                        finishGetLink();
                    }
                }
            }//ket thuc duyet megaupload
        }

        private void m_fakeWebBrowser_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
